package hczc.longcai.com.hccrowdfunding.home.fourth_mine.activities;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import hczc.longcai.com.hccrowdfunding.R;
import hczc.longcai.com.hccrowdfunding.activity.BaseActivity;
import hczc.longcai.com.hccrowdfunding.application.BaseApplication;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.adapters.AddressAdapter;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.AddIncome;
import hczc.longcai.com.hccrowdfunding.home.fourth_mine.beans.Address;
import hczc.longcai.com.hccrowdfunding.home.second_invest.activities.AddAddressActivity;

public class AddressManageActivity extends BaseActivity implements View.OnClickListener, AdapterView.OnItemClickListener {

    private static final String TAG = "AddressManageActivity";
    private ListView listView;
    private List<Address> list;
    private AddressAdapter adapter;
    private Button add;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_manage);

        initDatas();
        initViews();
    }

    private void initDatas() {
        list = new ArrayList<>();
        list = BaseApplication.dbAddressController.getAddress();


    }

    private void initViews() {
        findViewById(R.id.head_back).setOnClickListener(this);
        ((TextView) findViewById(R.id.head_title)).setText("收货地址管理");

        add = ((Button) findViewById(R.id.address_manage_button));
        add.setOnClickListener(this);

        listView = ((ListView) findViewById(R.id.mine_address_manage_listview));
        adapter = new AddressAdapter(this,list,1);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
            case R.id.head_back:
                finish();
                break;
            case R.id.address_manage_button:
                startActivityForResult(new Intent(AddressManageActivity.this, AddAddressActivity.class),2);
                break;
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 1){
            list.clear();
            list.addAll(BaseApplication.dbAddressController.getAddress());
            adapter.notifyDataSetChanged();
        }
        if (resultCode == 1 && requestCode == 2){
//            Log.d(TAG,"*(*(*(*(*(**(*(*(*((*(*((");
            list.clear();
            list.addAll(BaseApplication.dbAddressController.getAddress());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        Intent intent = new Intent(AddressManageActivity.this, EditAddressActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("address",list.get(position));
        intent.putExtras(bundle);
        startActivityForResult(intent,0);

    }


}
